package es.capgemini.extjsplugin.ui;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import es.capgemini.extjsplugin.wizard.AttributesCellModifier;

/**
 * Panel de controles para el AttributesSelectionPage
 * @author Lisandro Medrano
 *
 */
public class AttributesSelectionPanel extends org.eclipse.swt.widgets.Composite {
	
	public List<String> getColumnNames() {
		return Arrays.asList(columnNames);
	}

	public void setColumnNames(String[] columnNames) {
		this.columnNames = columnNames;
	}


	private String columnNames[]=new String[]{"!","Nombre","Tipo"};
	private TableViewer tableViewer;
	private Table table;

	
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/

	public AttributesSelectionPanel(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			createTable(this);
			createTableViewer();
			this.layout();
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Create the Table
	 */
	private void createTable(Composite parent) {
		int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | 
					SWT.FULL_SELECTION | SWT.HIDE_SELECTION;


		table = new Table(parent, style);
		
		GridData gridData = new GridData(GridData.FILL_BOTH);
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalSpan = 3;
		table.setLayoutData(gridData);		
					
		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		// 1st column with image/checkboxes - NOTE: The SWT.CENTER has no effect!!
		TableColumn column = new TableColumn(table, SWT.CENTER, 0);		
		column.setText("!");
		column.setWidth(20);
		
		// 2nd column with task Description
		column = new TableColumn(table, SWT.LEFT, 1);
		column.setText("Nombre");
		column.setWidth(100);
		


		// 3rd column with task Owner
		column = new TableColumn(table, SWT.LEFT, 2);
		column.setText("Tipo");
		column.setWidth(100);
		// Add listener to column so tasks are sorted by owner when clicked


	}
	/**
	 * Create the TableViewer 
	 */
	private void createTableViewer() {
		FormData tableViewerLData = new FormData();
		tableViewerLData.left =  new FormAttachment(0, 1000, 37);
		tableViewerLData.top =  new FormAttachment(0, 1000, 20);
		tableViewerLData.width = 391;
		tableViewerLData.height = 134;
		
		tableViewer = new TableViewer(table);
		tableViewer.setUseHashlookup(true);
		tableViewer.setColumnProperties(columnNames);
		tableViewer.getControl().setLayoutData(tableViewerLData);

		// Create the cell editors
		CellEditor[] editors = new CellEditor[columnNames.length];

		// Column 1 : Completed (Checkbox)
		editors[0] = new CheckboxCellEditor(table);

		// Column 2 : Description (Free text)
		TextCellEditor textEditor = new TextCellEditor(table);
		((Text) textEditor.getControl()).setTextLimit(60);
		editors[1] = textEditor;

		// Column 3 : Owner (Combo Box) 
		editors[2] = new ComboBoxCellEditor(table, new String[]{"Texto","Fecha","Entero"}, SWT.READ_ONLY);
 
		tableViewer.setCellEditors(editors);
		tableViewer.setCellModifier(new AttributesCellModifier(this));
	
	}
	
	public TableViewer getTableViewer() {
		return tableViewer;
	}

}
